﻿using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4.Shoppgin.Admin.API.Controllers
{
    [Route("month")]
    [ApiController]
    public class MonthController : ControllerBase
    {
        //模拟数据
        private static List<OrderMasterEntity> OrderMasters = new List<OrderMasterEntity>();
        private static List<OrderDetailEntity> OrderDetails = new List<OrderDetailEntity>();
        private static List<LogisticMasterEntity> LogisticMasters = new List<LogisticMasterEntity>();
        private static List<LogisticDetailEntity> LogisticDetails = new List<LogisticDetailEntity>();


        public MonthController()
        {
            if (OrderMasters.Count <= 0)
            {
                string orderNo1 = new Snowflake.Core.IdWorker(1, 2).NextId().ToString();
                OrderMasters.Add(
                    new OrderMasterEntity
                    {
                        Id = Guid.NewGuid(),
                        OrderNo = orderNo1,
                        OrderAmount = 99,
                        OrderState = Domain.Shared.OrderStateEnum.WaittingPayment,
                        OrderTime = System.DateTime.Now
                    });

                OrderDetails.Add(new OrderDetailEntity
                {
                    Id = Guid.NewGuid(),
                    OrderNo = orderNo1,
                    SKUName = "华为手机",
                    SKUPrice = "4999",
                    SKUNumber = "1"
                });
                OrderDetails.Add(new OrderDetailEntity
                {
                    Id = Guid.NewGuid(),
                    OrderNo = orderNo1,
                    SKUName = "小米手机",
                    SKUPrice = "5999",
                    SKUNumber = "2"
                });

                string orderNo2 = new Snowflake.Core.IdWorker(2, 2).NextId().ToString();
                OrderMasters.Add(
                   new OrderMasterEntity
                   {
                       Id = Guid.NewGuid(),
                       OrderNo = orderNo2,
                       OrderAmount = 199,
                       OrderState = Domain.Shared.OrderStateEnum.ToBeShipped,
                       OrderTime = System.DateTime.Now,
                       PayAmount = 199,
                       PayType = Domain.Shared.PayTypeEnum.WeiXinPay,
                       PayTime = System.DateTime.Now.AddMinutes(2)
                   });

                OrderDetails.Add(new OrderDetailEntity
                {
                    Id = Guid.NewGuid(),
                    OrderNo = orderNo2,
                    SKUName = "华为手机p60",
                    SKUPrice = new Random().Next(1000, 9999).ToString(),
                    SKUNumber = "1"
                });
                OrderDetails.Add(new OrderDetailEntity
                {
                    Id = Guid.NewGuid(),
                    OrderNo = orderNo2,
                    SKUName = "小米手机p34",
                    SKUPrice = new Random().Next(1000, 9999).ToString(),
                    SKUNumber = "2"
                });
            }
        }


        /// <summary>
        /// 获取订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetOrder(int state)
        {
            var list = from a in OrderMasters
                       select new
                       {
                           a.OrderNo,
                           amount = a.OrderAmount,
                           a.OrderState,
                           a.PayType,
                           items = OrderDetails.Where(x => x.OrderNo.Equals(a.OrderNo)).ToList()
                       };
            if (state >= 0)
            {
                 list = list.Where(x => state == (int)x.OrderState);
            }
            return Ok(list);
        }

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public IActionResult GetOrderDetail(string orderNo)
        {
            //查订单主表（包括：订单信息、收件人信息）
            //查订单明细（这个包括商品信息）
            //查物流

            var master = OrderMasters.Where(x => x.OrderNo.Equals(orderNo)).Select(x => new
            {
                x.OrderNo,
                x.OrderState,
                OrderTime = x.OrderTime.ToString("yyyy-MM-dd hh:mm:ss"),

            });
            var detail = OrderDetails.Where(x => x.OrderNo.Equals(orderNo));

            return Ok(new { master, detail });
        }
    }
}
